﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dto;
using Model;

namespace DVT.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/

        public ActionResult Index(string newsid)
        {

            using ( Model.NewsDBEntities entities = new NewsDBEntities())
            {
         
           
            if (string.IsNullOrEmpty(newsid))
            {
                var news = entities.News.ToList();
                 
                IList<NewsDto> newsDto = new List<NewsDto>();

                foreach (News n in news)
                {
                    NewsDto newsdto = new NewsDto();
                    newsdto.Id = n.Id;
                    newsdto.Content = n.NewsContent;
                    newsdto.Title = n.Title;
                    newsdto.PostDate = n.PostDate;
                    newsDto.Add(newsdto);
                }

                ViewBag.NewsList = newsDto;
            }
            else
            {
                var newinstance = entities.News.Where(x => x.Id == newsid).ToList();
                if (newinstance.Count > 0)
                {
                    var d = newinstance[0].NewsContent;
                    ViewBag.Content = d;
                    ViewBag.NTitle = newinstance[0].Title;
                }
            }

            }
            return View();
        }

    }
}
